package com.njupt.content.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.njupt.common.pojo.EasyUIDataGridResult;
import com.njupt.common.pojo.JsonUtils;
import com.njupt.common.pojo.TaotaoResult;
import com.njupt.content.service.ItemContentService;
import com.njupt.jedis.JedisClient;
import com.njupt.mapper.TbContentMapper;
import com.njupt.pojo.TbContent;
import com.njupt.pojo.TbContentExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;


import java.util.Date;
import java.util.List;

/**
 * @Author: XJL
 * @Description:
 * @Date: Create in 10:13 2018/5/29
 * @Modified By:
 **/
@Service
public class ItemContentServiceImpl implements ItemContentService{

    @Autowired
    private TbContentMapper tbContentMapper;

    @Autowired
    private JedisClient jedisClient;

    @Value("${INDEX_CONTENT}")
    private String INDEX_CONTENT;

    @Value("${EXPIE_TIME}")
    private Integer EXPIE_TIME;

    @Override
    public EasyUIDataGridResult getContentByPage(long categoryId, int pages, int rows) {

       // PageHelper.startPage(pages,rows);
        TbContentExample example = new TbContentExample();
        TbContentExample.Criteria criteria = example.createCriteria();

        criteria.andCategoryIdEqualTo(categoryId);

        List<TbContent> list = tbContentMapper.selectByExample(example);
      //  System.out.println(list);
//        for (TbContent content:list
//             ) {
//            System.out.println(content);
//        }
        EasyUIDataGridResult result = new EasyUIDataGridResult();
        if (list!=null){
            PageInfo<TbContent> pageInfo = new PageInfo<TbContent>(list);
            result.setTotal(pageInfo.getTotal());
            result.setRows(list);
        }
        return result;
    }

    @Override
    public List<TbContent> getContentByCid(long cid) {
        //添加缓存
        try {
            //1、查询缓存
           String json = jedisClient.hget(INDEX_CONTENT,cid+"");
              if(org.apache.commons.lang3.StringUtils.isNotBlank(json)){
                   return JsonUtils.jsonToList(json,TbContent.class);
              }

        }catch (Exception e){
            e.printStackTrace();
        }

        //缓存中未查询到，查数据库
        //数据添加到缓存
        TbContentExample example = new TbContentExample();
        TbContentExample.Criteria criteria = example.createCriteria();
        criteria.andCategoryIdEqualTo(cid);
        List<TbContent> list = tbContentMapper.selectByExample(example);
        try {
                jedisClient.hset(INDEX_CONTENT,cid+"",JsonUtils.objectToJson(list));
                jedisClient.expire(INDEX_CONTENT,EXPIE_TIME);
        }   catch (Exception e){
            e.printStackTrace();
        }

        return list;
    }

    @Override
    public TaotaoResult addContent(TbContent content) {
        try {
            jedisClient.hdel(INDEX_CONTENT,content.getCategoryId()+"");
            System.out.println(content.getCategoryId()+"+++++++++++++++++");
        }catch (Exception e){
            e.printStackTrace();
        }
        content.setCreated(new Date());
        content.setUpdated(new Date());
        tbContentMapper.insert(content);
        return TaotaoResult.ok();
    }


}
